package cate.game.role.farm.adventure;

import cate.common.table.d.GDFight;
import cate.common.table.d.GDOperation;
import cate.common.table.farm.adventure.row.AdventureCharpterRow;
import cate.game.client.msg.MergeMsgSender;
import cate.game.play.proce.play.PlayPO;
import cate.game.play.vo.PlayReportVO;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarmFTC;
import cate.game.role.farm.adventure.msg.AdventureDataResp;
import cate.game.role.farm.adventure.msg.AdventureResultResp;
import easy.java.practice.errorcode.EcResult;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class AdventureFTC extends AbstractFarmFTC<Adventure> {

    /**
     * 当前挑战的战斗关卡模板
     */
    private final AdventureCharpterRow tpl;


    public AdventureFTC(Role role, AdventureCharpterRow tpl,byte posNum) {
        setup(role);
        this.tpl = tpl;
        super.posNum = posNum;
    }

    @Override
    protected Adventure po(Role role) {
        return role.getFarm().adventure;
    }

    @Override
    public void handleResult(Role role, PlayPO playPO) {
        super.handleResult(role, playPO);
        boolean win = playPO.winner == GDFight.Team.A;
        MixRes reward = null;
        MergeMsgSender sender = new MergeMsgSender();
        Adventure po = po(role);
        final byte nextPosNum = (byte) (super.posNum + 1);
        //是否还要继续挑战
        boolean autoEnter = false;
        //之前的战报
        List<PlayReportVO> reports = new ArrayList<>();

        if (win) {
            autoEnter = StringUtils.isNotBlank(tpl.getInsCode(nextPosNum));
            //打完了
            if (!autoEnter) {
                po.changeCombatIdPassed(tpl.id);
                role.log.setOperation(GDOperation.ADVENTURE_CHALLENGE);
                reward = po.buildReward();
                reward.addList(new MixRes(tpl.sectionResource));
                reward.add(role, sender);
                role.log.clearOperation();
                role.getHistory().action.adventurePass();
                role.send(new AdventureDataResp(role));
                List<PlayPO> plays = role.getGame().farm.local.adventure.tryUpdate(role, playPO, tpl.id);
                for (PlayPO play : plays) {
                    reports.add(new PlayReportVO(play));
                }
            } else {
                //还没打完 先缓存一下 这一场的录像
                role.getGame().farm.local.adventure.recordPlay(role.getUid(), playPO);
            }
        }else {
            //打输了要把之前的录像战报发过去
            List<PlayPO> plays = role.getGame().farm.local.adventure.removePlay(role.getUid());
            for (PlayPO play : plays) {
                reports.add(new PlayReportVO(play));
            }
            //前端一定要把当前战斗的战报也发过去？？不理解
            reports.add(new PlayReportVO(playPO));
        }
        AdventureResultResp result = new AdventureResultResp(tpl.id, win, reward);
        result.over = !autoEnter;
        result.reports = reports;
        role.sendNow(result);
        sender.send(role, true);

        if (autoEnter) {
            EcResult r = po.doChallenge(tpl, nextPosNum);
            if (!r.ok()) {
                role.getGame().farm.local.adventure.removePlay(role.getUid());
            }
        }
    }
}
